package nxu.it.service;

import nxu.it.entity.Category;
import nxu.it.entity.Comment;
import nxu.it.entity.Post;
import nxu.it.entity.User;
import org.jdbi.v3.core.Jdbi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author yangliu
 * @project jfinal-app-1
 * @sid 12020243892
 * @date 2022/12/5 - 20:33
 */
public class UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger("nxu.it");
    Jdbi jdbi = DbHelper.JDBI;

    public boolean register(User user) {

        try {
            String insertSql = "INSERT INTO t_user(sno, password, department_id, grade, major) " +
                    "VALUES(:sno, :password, :department_id, :grade, :major) ";
            DbHelper.JDBI.withHandle(handle -> handle.createUpdate(insertSql).bindBean(user).execute());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("注册失败");
            return false;
        }
    }

    public boolean checkLogin(User user) {
        String sql="select count(*) from t_user where sno= ? and  password = ? ";
        Integer count= DbHelper.JDBI.withHandle(handle -> handle.createQuery(sql).bind(0,user.getSno())
                .bind(1,user.getPassword())
                .mapTo(Integer.class)
                .one()
        );
        return count!=0;
    }



}
